package com.apicloud.modulefloatwebview;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.uzmap.pkg.EntranceActivity;
import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONException;
import org.json.JSONObject;

import static android.provider.Settings.canDrawOverlays;

public class Dome extends UZModule {

    private UZModuleContext createContext;
    private static final int REQUEST_CODE = 0;
    public Dome(UZWebView webView) {
        super(webView);

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode != REQUEST_CODE) return;
        if (Build.VERSION.SDK_INT >= 23 && !canDrawOverlays(context())) {
            try {
                JSONObject ret = new JSONObject();
                ret.put("status",false).put("msg","授权失败");
                createContext.success(ret,true);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            create();
        }
    }
    private void create() {
        UZModuleContext moduleContext = createContext;
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        String url = moduleContext.optString("url",null);
        if (url.indexOf("http://")!=0)url = makeRealPath(url);

        FloatWebView floatWebView = new FloatWebView(context(),moduleContext,name,url);
        int w = moduleContext.optInt("w",0);
        int h = moduleContext.optInt("h",0);
        int x = moduleContext.optInt("x",0);
        int y = moduleContext.optInt("y",0);
        if (w!=0 || h!=0) floatWebView.viewSize(w,h);
        if (x!=0 || y!=0) floatWebView.offsetXY(x,y);
        floatWebView.noTouch(moduleContext.optBoolean("noTouch",false));
        floatWebView.noLimit(moduleContext.optBoolean("noLimit",false));
        floatWebView.center(moduleContext.optBoolean("center",false));
        if (moduleContext.optBoolean("followTouch",false)){
            floatWebView.followTouch(true);
            if (moduleContext.optBoolean("stickSide",false)){
                floatWebView.stickSide(true);
            }
        }
        try {
            ret.put("status",true).put("msg","OK");
            moduleContext.success(ret,false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void jsmethod_create(UZModuleContext moduleContext) throws JSONException {
        createContext = moduleContext;
        if (Build.VERSION.SDK_INT >= 23 && !canDrawOverlays(context())) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + context().getPackageName()));
            startActivityForResult(intent, REQUEST_CODE);
            JSONObject ret = new JSONObject();
            ret.put("status",false).put("msg","请授权");
            createContext.success(ret,true);
            return;
        }
        create();
    }
    public void jsmethod_offset(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        int x = moduleContext.optInt("x",0);
        int y = moduleContext.optInt("y",0);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.offsetXY(x,y);
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_size(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        int w = moduleContext.optInt("w",0);
        int h = moduleContext.optInt("h",0);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.viewSize(w,h);
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_noTouch(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.noTouch(moduleContext.optBoolean("noTouch",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_noLimit(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.noLimit(moduleContext.optBoolean("noLimit",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_center(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.center(moduleContext.optBoolean("center",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_canFocus(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.canFocus(moduleContext.optBoolean("canFocus",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_followTouch(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.followTouch(moduleContext.optBoolean("followTouch",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_stickSide(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.stickSide(moduleContext.optBoolean("stickSide",false));
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    //-----------------------操作系列方法-------------------------
    public void jsmethod_show(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);

        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.show();
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_hide(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.hide();
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_remove(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            floatWebView.remove();
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
    public void jsmethod_eval(UZModuleContext moduleContext) throws JSONException {
        JSONObject ret = new JSONObject();
        String name = moduleContext.optString("name",null);
        if (FloatWebView.windowList.containsKey(name)){
            FloatWebView floatWebView = FloatWebView.windowList.get(name);
            String jsstring = moduleContext.optString("jsstring",null);
            Log.e("jsstring",jsstring);
            if(Build.VERSION.SDK_INT >= 19){
                if(jsstring!=null) floatWebView.webView.evaluateJavascript(jsstring,null);
            }else{
                if(jsstring!=null) floatWebView.webView.loadUrl("javascript:" + jsstring + ";");
            }
            ret.put("status",true);
            ret.put("msg","OK");
        }else{
            ret.put("status",false);
            ret.put("msg","悬浮窗不存在");
        }
        moduleContext.success(ret,true);
    }
}
